const { BrowserWindow, ipcMain, dialog, globalShortcut, Menu } = require('electron');
const Wallpaper = require('../utils/wallpaper');
class DrawWindow {

    constructor() {
        this.window = null;
        this.isShow = false;
    }

    createWindow() {
        Menu.setApplicationMenu(null);
        this.window = new BrowserWindow({
            width : 1200,
            height : 800,
            minWidth : 1024,
            minHeight : 768,
            show : false,
            webPreferences : {
                devTools : true,
                nodeIntegration : true
            }
        });
        this.window.loadURL(`file://${__dirname}/../../dist/index.html`);
        //this.window.loadURL(`http://127.0.0.1:8086/index.html`);
    }

    show () {
        this.window.show();
        this.window.focus();
        this.isShow = true;
    }

    shortcut() {
        globalShortcut.register('CommandOrControl+Alt+D', () => {
            this.window.webContents.openDevTools()
        })
    }

    ipc () {
        ipcMain.on('set-wallpaper', (event, arg) => {
            if (!arg || arg.localPath.length <= 0) {
                event.reply('set-wallpaper-reply', {result: 'fail', msg: 'empty image'});
                return;
            }
            (new Wallpaper()).setWallpaper(arg.localPath);
            event.reply('set-wallpaper-reply', {result: 'success', data: arg});
        });
        ipcMain.on('select-download-dir', (event) => {
            let dir = dialog.showOpenDialogSync(this.window, {
                properties: ['openDirectory']
            });
            event.reply('select-download-dir-reply', dir);
        });
    }
}

module.exports = DrawWindow;
